#!/bin/bash

# Rust 综合演示脚本
# 展示 Rust 在各个领域的应用

echo "🦀 Rust 综合演示项目"
echo "====================="
echo ""

# 检查 Rust 环境
echo "📋 检查 Rust 环境..."
rustc --version
cargo --version
echo ""

# 编译所有项目
echo "🔨 编译所有项目..."
cargo build --release
echo ""

# 1. 系统级软件演示
echo "1️⃣ 系统级软件演示"
echo "=================="
cd system-software
cargo run
cd ..
echo ""

# 2. CLI 工具演示
echo "2️⃣ 命令行工具演示"
echo "=================="
cd cli-tool

echo "📊 文件统计演示:"
cargo run -- file stats . --recursive
echo ""

echo "🔍 文件搜索演示:"
cargo run -- file search "*.rs" --directory .
echo ""

echo "📝 文本统计演示:"
cargo run -- text count README.md
echo ""

echo "🖥️ 系统信息演示:"
cargo run -- system info
echo ""

cd ..
echo ""

# 3. 网络服务演示
echo "3️⃣ 网络服务演示"
echo "=================="
echo "🚀 启动网络服务器 (后台运行)..."
cd network-service
cargo run &
SERVER_PID=$!
sleep 3

echo "📡 测试 API 端点:"
echo "获取用户列表:"
curl -s http://127.0.0.1:8080/users | head -c 200
echo ""
echo ""

echo "创建用户:"
curl -s -X POST http://127.0.0.1:8080/users \
  -H "Content-Type: application/json" \
  -d '{"name":"测试用户","email":"test@example.com"}' | head -c 200
echo ""
echo ""

echo "获取性能统计:"
curl -s http://127.0.0.1:8080/stats | head -c 200
echo ""
echo ""

# 停止服务器
kill $SERVER_PID
cd ..
echo ""

# 4. WebAssembly 演示
echo "4️⃣ WebAssembly 演示"
echo "===================="
echo "📦 编译 WebAssembly 模块..."
cd wasm-demo
if command -v wasm-pack &> /dev/null; then
    wasm-pack build --target web
    echo "✅ WebAssembly 模块编译完成"
    echo "🌐 在浏览器中打开 index.html 查看演示"
else
    echo "⚠️  未安装 wasm-pack，跳过 WebAssembly 演示"
    echo "   安装命令: cargo install wasm-pack"
fi
cd ..
echo ""

# 5. 嵌入式系统演示
echo "5️⃣ 嵌入式系统演示"
echo "=================="
echo "🔧 编译嵌入式系统代码..."
cd embedded-demo
cargo check
echo "✅ 嵌入式系统代码编译通过"
echo "📝 注意: 这是 no_std 环境，需要目标平台工具链才能实际运行"
cd ..
echo ""

# 总结
echo "🎉 演示完成！"
echo "============="
echo ""
echo "✨ Rust 在各个领域的优势:"
echo "   • 系统编程: 内存安全 + 零成本抽象"
echo "   • 网络服务: 异步 I/O + 类型安全"
echo "   • CLI 工具: 高性能 + 跨平台"
echo "   • WebAssembly: 接近原生性能"
echo "   • 嵌入式: no_std + 硬件抽象"
echo ""
echo "📚 学习资源:"
echo "   • Rust 官方文档: https://doc.rust-lang.org/"
echo "   • Rust by Example: https://doc.rust-lang.org/rust-by-example/"
echo "   • Rustlings 练习: https://github.com/rust-lang/rustlings"
echo ""
echo "Happy Coding with Rust! 🦀✨"


